<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>icf</title>

	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
	<link rel="stylesheet" href="/public/layui/admin/layui/css/layui.css" media="all">
	<link rel="stylesheet" href="/public/layui/admin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-card-header">{{.StructName}}</div>
		<div class="layui-card-body" style="padding: 15px;">
			<form class="layui-form" action="" lay-filter="component-form-group">
				{% if data.detail.Id %}
				<input name="id" type="hidden" value="{{.L2Brace}} data.detail.Id {{.R2Brace}}"/>
				{% endif %}

				<!-- todo #FormFieldsHtml# -->
				{{template "FormFieldsHtml_template" .}}

				<div class="layui-form-item layui-layout-admin">
					<div class="layui-input-block">
						<div class="layui-footer" style="left: 0;">
							<button class="layui-btn" lay-submit="" lay-filter="edit-submit">提交</button>
							<a href="javascript:goBack()" class="layui-btn layui-btn-primary">返回</a>
						</div>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>


<script src="/public/layui/admin/layui/layui.js"></script>
<script>
	layui.config({
		base: '/public/layui/admin/' //静态资源所在路径
	}).extend({
		index: 'lib/index' //主入口模块
	}).use(['index', 'form', 'laydate','jquery','upload'], function(){
		var $ = layui.$
				,admin = layui.admin
				,element = layui.element
				,layer = layui.layer
				,laydate = layui.laydate
				,form = layui.form;

		form.render(null, 'component-form-group');
		laydate.render({
			elem: '#LAY-component-form-group-date'
		});

		/* 自定义验证规则 */
		form.verify({
			title: function(value){
				if(value.length < 0){
					return '请输入';
				}
			}
			,pass: [/(.+){6,12}$/, '密码必须6到12位']
			,content: function(value){
				layedit.sync(editIndex);
			}
		});

		// 文件上传
		var upload = layui.upload;
		upload.render({
			elem: '#upload-drag',
			url: '/admin/upload',
			done: function(res) {
				console.log(res)
				$("#img-upload").attr("src", res.data.imgurl);
				// TODO: $("#prdminipic").val(res.data.imgurl);
			}
		});

		/* 监听提交 */
		form.on('submit(edit-submit)', function(data){
			// parent.layer.alert(JSON.stringify(data.field), {
			//   title: '最终的提交信息'
			// })
			$.ajax({
				type: "POST",
				url : "/admin/{{.ModuleName}}/{{.StructName}}/editpage",
				data: data.field,
				async: true,
				error: function(request) {
					parent.layer.alert(JSON.stringify(request), {title: '操作失败'});
				},
				success: function(response) {
					console.log(response);
					//var rt = JSON.parse(response);
					var rt = response
					if(rt.code == 0){
						var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
						parent.layer.close(index);
					}else{
						parent.layer.alert(rt.msg, {title: '操作失败'});
					}
				}
			});
			return false;
		});

		/* 监听指定开关
        form.on('switch(component-form-switchTest)', function(data){
          layer.msg('开关checked：'+ (this.checked ? 'true' : 'false'), {
            offset: '6px'
          });
          layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        });*/
	});

	// 页面返回上一页面
	function goBack(){
		var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
		parent.layer.close(index);
	};

</script>
</body>
</html>

{{define "FormFieldsHtml_template"}}
	{{range $index, $element := .FieldList}} {{if not .IsShowTitle}} <!-- {{end}} {{if eq .TitleType "input"}}  {{template "input_template" $element}}
	    {{else if eq .TitleType "check"}}  {{template "check_template" $element}}
		{{else if eq .TitleType "radio"}}  {{template "radio_template" $element}}
	    {{else if eq .TitleType "select"}}  {{template "select_template" $element}}
		{{else if eq .TitleType "textarea"}}  {{template "textarea_template" $element}}
	    {{else if eq .TitleType "upload"}}  {{template "upload_template" $element}}
	    {{else}}  {{template "input_template" $element}}  {{end}}  {{if not .IsShowTitle}} --> {{end}}
	{{end}}
{{end}}


{{define "input_template"}}
<div class="layui-form-item">
	<label class="layui-form-label">{{.TitleName}}</label>
	<div class="layui-input-block">
		<input type="text"
			   id="{{.FieldNameSnaked}}"
			   name="{{.FieldNameSnaked}}"
			   value="{{.L2Brace}} data.detail.{{.FieldName}} {{.R2Brace}}"
			   lay-verify="title" autocomplete="off" placeholder="" class="layui-input" {{if .IsOnlyRead}} disabled=""{{end}}>
	</div>
</div>
{{end}}

{{define "check_template"}}
<div class="layui-form-item">
	<label class="layui-form-label">{{.TitleName}}</label>
	<div class="layui-input-block">
		<input type="checkbox"
			   id="{{.FieldNameSnaked}}"
			   name="{{.FieldNameSnaked}}"
			   value="{{.L2Brace}} data.detail.{{.FieldName}} {{.R2Brace}}"
			   lay-skin="primary"
			   lay-filter=""
			   title="val1" checked>
	</div>
</div>
{{end}}

{{define "radio_template"}}
<div class="layui-form-item">
	<label class="layui-form-label">{{.TitleName}}</label>
	<div class="layui-input-block">
		<input type="radio" name="{{.FieldNameSnaked}}" value="1"  title="val1" {% if data.detail.{{.FieldName}} == 1 %}checked=""{%endif%}>
		<input type="radio" name="{{.FieldNameSnaked}}" value="2"  title="val2" {% if data.detail.{{.FieldName}} == 2 %}checked=""{%endif%}>
	</div>
</div>
{{end}}

{{define "select_template"}}
<div class="layui-form-item">
	<label class="layui-form-label">{{.TitleName}}</label>
	<div class="layui-input-block">
		<select id="{{.FieldNameSnaked}}" name="{{.FieldNameSnaked}}">
			<option value="">请选择</option>
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
			<option value="3">4</option>
		</select>
	</div>
</div>
{{end}}


{{define "textarea_template"}}
<div class="layui-form-item">
	<label class="layui-form-label">{{.TitleName}}</label>
	<div class="layui-input-block">
		<textarea id="{{.FieldNameSnaked}}" name="{{.FieldNameSnaked}}" placeholder="" class="layui-textarea" {{if .IsOnlyRead}} disabled=""{{end}}>{{.L2Brace}} data.detail.#FieldName# {{.R2Brace}}</textarea>
	</div>
</div>
{{end}}

{{define "upload_template"}}
<div class="layui-form-item">
	<label class="layui-form-label">{{.TitleName}}</label>
	<div class="layui-input-block">
		<input type="hidden"
			   id="{{.FieldNameSnaked}}"
			   name="{{.FieldNameSnaked}}"
			   value="{{.L2Brace}} data.detail.{{.FieldName}} {{.R2Brace}}" />
		<div class="layui-upload-drag" id="upload-drag">
			<i class="layui-icon"></i>
			<p>点击上传图片</p>
			<img id="img-upload" src="{{.L2Brace}} data.detail.{{.FieldName}} {{.R2Brace}}" style="width: 100%">
		</div>
	</div>
</div>
{{end}}